﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart"
    xmlns:converters="clr-namespace:De.LarsHildebrandt.WpfControls.WpfSimpleChart.Converters">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/De.LarsHildebrandt.WpfControls.WpfSimpleChart;component/ResourceDictionaries/ChartToolTip.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <converters:PieChartHeightConverter x:Key="pieChartHeightConverter"/>
    <converters:PieChartWidthConverter x:Key="pieChartWidthConverter"/>
    <converters:PieChartCenterConverter x:Key="pieChartCenterConverter"/>

    <Style TargetType="{x:Type local:PieChart}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:PieChart}">
                    <Border
                        x:Name="pieChartBorder"
                        Background="{Binding Path=ChartBackground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"
                        BorderBrush="{Binding Path=ChartBackgroundBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"
                        BorderThickness="{Binding Path=ChartBackgroundBorderThickness, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"
                        CornerRadius="{Binding Path=ChartBackgroundCornerRadius, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"
                        >
                        <Grid x:Name="PieChartGrid">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Label Grid.Row="0" HorizontalAlignment="Center" FontSize="12">
                                <Label.Content>
                                    <Binding Path="Caption" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}"/>
                                </Label.Content>
                            </Label>
                            <ItemsControl 
                                    x:Name="itemsControl" 
                                    ItemsSource="{Binding}"
                                    Margin="5"
                                    Grid.Row="1">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <Grid IsItemsHost="True">

                                        </Grid>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <local:PiePiece
                                            x:Name="pie"
                                            Stroke="{Binding Path=ChartBorderColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"                                               
                                            Fill="{Binding Path=ChartBrush, UpdateSourceTrigger=PropertyChanged}"
                                            Id="{Binding Path=Id}"
                                            Value="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
                                            Caption="{Binding Path=Caption, UpdateSourceTrigger=PropertyChanged}"
                                            Items="{Binding Path=Items, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}, AncestorLevel=1}, UpdateSourceTrigger=PropertyChanged}"
                                            ClientHeight="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, UpdateSourceTrigger=PropertyChanged}"
                                            ClientWidth="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}, AncestorLevel=1}, UpdateSourceTrigger=PropertyChanged}"
                                            >
                                            <local:PiePiece.ToolTip>                                                
                                                <ToolTip Style="{StaticResource toolTipStyle}" />                                                
                                            </local:PiePiece.ToolTip>
                                        </local:PiePiece>
                                        <DataTemplate.Triggers>
                                            <Trigger SourceName="pie" Property="IsMouseOver" Value="True">
                                                <Setter 
                                                    TargetName="pie" 
                                                    Property="Fill" 
                                                    Value="{Binding Path=ChartColorMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}">
                                                </Setter>
                                            </Trigger>
                                        </DataTemplate.Triggers>
                                    </DataTemplate>                                    
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                            <ItemsControl
                                Grid.Row="2"
                                Margin="5"
                                Visibility="{Binding Path=ChartLegendVisibility, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}"
                                ItemsSource="{Binding Path=ChartLegend, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PieChart}, AncestorLevel=1}}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel IsItemsHost="True">

                                        </WrapPanel>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <Rectangle Stroke="Black" StrokeThickness="0.5" Height="10" Width="20" Fill="{Binding Path=ItemBrush}"/>
                                            <Label Content="{Binding Path=Caption}"></Label>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="HasDropShadow" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToolTip">
                    <Border Name="Border"
                        CornerRadius="4"
                        Background="{StaticResource LightBrush}"
                        BorderBrush="{StaticResource SolidBorderBrush}"
                        BorderThickness="1"
                        Width="{TemplateBinding Width}"
                        Height="{TemplateBinding Height}">
                        <StackPanel Orientation="Horizontal" >
                            <Label Content="{Binding Path=Caption}"></Label>
                            <Label>:</Label>
                            <Label Content="{Binding Path=Value}"></Label>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasDropShadow" Value="true">
                            <Setter TargetName="Border" Property="CornerRadius" Value="4"/>
                            <Setter TargetName="Border" Property="SnapsToDevicePixels" Value="true"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>